import pool_util from "./pool_util";
import Cell from "./cell";
import GameController, { GameState } from "./game_controller";
import { ParticleName } from "./particle_group";
import anim_util from "./anim_util";
import constants from "./constants";

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

var CellG = cc.Class({
    name:'CellG',
    properties: {
        name:'',
        initPollCount:0,
        prefab:cc.Prefab,
    }
});

@ccclass
export default class CellGroup extends cc.Component {

    @property({type:CellG})
    cellG=[];
 
    selectCell:cc.Node=null;

    selectAction=null;
 

    @property([cc.SpriteFrame])
    cellSprites:cc.SpriteFrame[]=[];
  
    cellSpritesIndex={
        2:0,
        4:1,
        8:2,
        16:3,
        32:4,
        64:5,
        128:6,
        256:7,
        512:8,
        1024:9,
        2018:10
    }

    cellSpritesColor={
        0:cc.color(255,21,21,255),
        1:cc.color(29,153,255,255),
        2:cc.color(255,233,34,255),
        3:cc.color(153,204,51,255),
        4:cc.color(51,51,153,255),
        5:cc.color(62,249,227,255),
        6:cc.color(123,23,193,255),
        7:cc.color(255,62,122,255),
        8:cc.color(209,199,183,255),
        9:cc.color(33,31,32,255),
        10:cc.color(241,90,31,255)
    }

    getCellColorByValue(value){
        return this.cellSpritesColor[this.cellSpritesIndex[value]];
    }



    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        pool_util.batchInitObjPool(this,this.cellG);
     }

    start () {

    }
 
    getCellSprite(value:number){
        if(value>=2018){
            return this.cellSprites[this.cellSpritesIndex[2018]];
        }
        return this.cellSprites[this.cellSpritesIndex[value]];
    }

    cancelSelect(){
        this.selectCell=null;
        this.unschedule(this.runSelectAnim);
    }

    select(cell:cc.Node){
        cc.log(GameController.getInst().gameState);
        if(GameController.getInst().gameState!==GameState.Free){
            return;
        }
        if(this.selectCell===cell){
            return;
        }
        // GameController.getInst().runTipTurtorialStart();
        cc.director.emit(constants.GAME_EVENT.BtnClick);
        if(this.selectCell!==null){
            this.selectCell.scale=1;
            this.selectCell.opacity=255;
        }

        this.selectCell=cell;
        this.unschedule(this.runSelectAnim);
        this.runSelectAnim();
        this.unschedule(this.runSelectAnim);
        this.schedule(this.runSelectAnim,1);
    }

    runSelectAnim(){
        if(this.selectAction){
            this.selectCell.stopAction(this.selectAction);
        }
        this.selectAction= cc.sequence(cc.spawn(cc.scaleTo(0.5, 0.9, 0.9),cc.fadeTo(0.5, 180)),cc.spawn(cc.scaleTo(0.5, 1.1, 1.1),cc.fadeTo(0.5, 255)));
        this.selectCell.runAction(this.selectAction);
    }

    died(node){
        
        pool_util.backObjPool(this,node);
    }
 

    getRandomValue(){
        let values=[2,4,8,16,32];
        return values[Math.floor(Math.random()*values.length)];
    }


    setParticleNode(pos,value){
        let particleNode= GameController.getInst().particleGroup.getNewNode(ParticleName.Bomb1);
 
        let particleSys:cc.ParticleSystem= particleNode.getComponent(cc.ParticleSystem);
        particleSys.startColor=this.cellSpritesColor[this.cellSpritesIndex[value]];
        particleSys.endColor=this.cellSpritesColor[this.cellSpritesIndex[value]];
 
        particleNode.setPosition(pos);

        particleSys.resetSystem();
 
    }
   
 
 
    createCell(cellBg:cc.Node,value){
        var poolName = this.cellG[0].name + 'Pool';
        var newNode = pool_util.genNewNode(this[poolName],this.cellG[0].prefab,this.node);
        newNode.getComponent(Cell).init(value);
        newNode.setPosition(cellBg.getPosition());
        this.setParticleNode(cellBg.getPosition(),value);

        newNode.scale=0;
        newNode.runAction(cc.sequence(cc.delayTime(0.5),cc.scaleTo(0.5, 1.2, 1.2), cc.scaleTo(0.5, 1, 1)));

        newNode.getComponent("cell").colRowPos=cellBg.getComponent("cell_bg").colRowPos;
        cellBg.getComponent("cell_bg").cell=newNode;
        GameController.getInst().cellBgGroup.cellMap[newNode.getComponent("cell").colRowPos.x][newNode.getComponent("cell").colRowPos.y]=1;

        return newNode;      
    }

  
 

    // update (dt) {}
}
